﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUS.ISS.ARS.BusinessEntity;
using NUS.ISS.ARS.BusinessEntity.ViewModels;
using NUS.ISS.ARS.Common;
using System.Data.Entity;

namespace NUS.ISS.ARS.BusinessComponent
{
    public class ReportBC
    {
        private static readonly ReportBC _instance = new ReportBC();

        public static ReportBC Instance
        {
            get
            {
                return _instance;
            }
        }

        public IEnumerable<FlightReportView> GenerateFlightReport(string flightNumber, string airportFrom, string airportTo)
        {
            IEnumerable<FlightReportView> flightReportView = null;

            using (var ent = new Entities())
            {
                flightReportView = ent.FlightBookingTripDetails
                        .Include(a => a.Passenger)
                        .Include(a => a.FlightBookingTrip.FlightSchedule)
                            .Where(a => a.FlightBookingTrip.FlightSchedule.FlightNumber.Contains(flightNumber)
                                        && a.FlightBookingTrip.FlightSchedule.Airport.AirportName.Contains(airportFrom)
                                        && a.FlightBookingTrip.FlightSchedule.Airport1.AirportName.Contains(airportTo))
                        .Include(a => a.FlightBookingTrip.FlightBooking.Customer)
                        .Select(t => new FlightReportView()
                        {
                            FlightNumber = t.FlightBookingTrip.FlightSchedule.FlightNumber,
                            AirportFrom = t.FlightBookingTrip.FlightSchedule.Airport.AirportName,
                            AirportTo = t.FlightBookingTrip.FlightSchedule.Airport1.AirportName,
                            CustomerId = t.FlightBookingTrip.FlightBooking.Customer.CustomerId,
                            CustomerName = t.FlightBookingTrip.FlightBooking.Customer.FirstName + " "
                                + t.FlightBookingTrip.FlightBooking.Customer.LastName,
                            CustomerContact = t.FlightBookingTrip.FlightBooking.Customer.ContactNumber,
                            PassengerName = t.Passenger.FirstName + " " + t.Passenger.LastName
                        }).OrderBy(a => a.FlightNumber).ToList();
            }

            return flightReportView;
        }

        public IEnumerable<RevenueReportView> GenerateRevenueReport (string flightNumber, string airportFrom, string airportTo)
        {
            IEnumerable<RevenueReportView> revenueReportView = null;

            using (var ent = new Entities())
            {
                revenueReportView = ent.FlightBookingTripDetails
                                        .Include(a => a.FlightBookingTrip.FlightSchedule)
                                            .Where(b => b.FlightBookingTrip.FlightSchedule.FlightNumber.Contains(flightNumber)
                                                    && b.FlightBookingTrip.FlightSchedule.Airport.AirportName.Contains(airportFrom)
                                                    && b.FlightBookingTrip.FlightSchedule.Airport1.AirportName.Contains(airportTo))
                                        .GroupBy(key => new
                                                    {
                                                        FlightNumber = key.FlightBookingTrip.FlightSchedule.FlightNumber, 
                                                        AirportFrom =  key.FlightBookingTrip.FlightSchedule.Airport.AirportName,
                                                        AirportTo = key.FlightBookingTrip.FlightSchedule.Airport1.AirportName
                                                    },
                                                 group => new 
                                                    {
                                                        Revenue = group.Price + group.Surcharge.Value
                                                    })
                                        .Select(t => new RevenueReportView()
                                        {
                                            FlightNumber = t.Key.FlightNumber,
                                            AirportFrom = t.Key.AirportFrom,
                                            AirportTo = t.Key.AirportTo,
                                            Revenue = t.Sum(s => s.Revenue)
                                        }).ToList();
            }

            return revenueReportView;
        }

    }
}
